package com.chocho.producerserver.controller;

import com.chocho.producerserver.bean.User;
import com.chocho.producerserver.service.UserService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/12.
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //统计用户数
    @PreAuthorize("hasAuthority('user:count')")
    @GetMapping("/count")
    @ResponseBody
    public Msg getUserCount(){
        return Msg.success().add("count", userService.count());
    }

    //查询用户信息
    @PreAuthorize("hasAuthority('user:count')")
    @PostMapping("/select")
    @ResponseBody
    public Msg selectUser(@RequestBody Map<String, Object> map){
        List<User> userList = userService.getUserByQuery(map);
        return (userList != null)
                ? Msg.success().add("userList", userList)
                : Msg.fail();
    }

    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('user:count')")
    @ResponseBody
    public Msg deleteUser(String ids){
        if(ids != null && !"".equals(ids)){
            return userService.deleteUser(ids) ? Msg.success() : Msg.fail();
        }
        return Msg.fail();
    }

    @GetMapping("/update/pwd")
    @ResponseBody
    public Msg updatePassword(HttpServletRequest request, String newPwd){
        return userService.updatePwd(request, newPwd) ? Msg.success() : Msg.fail();
    }
}
